package com.system.demo.commonService;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.system.demo.entity.database.UserNotifications;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class RabbitMQService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ObjectMapper objectMapper; // Jackson ObjectMapper，用于对象转JSON

    // 发送通知消息
    public void sendNotification(UserNotifications notification) {
        try {
            // 将实体类对象转换为 JSON 字符串
            String message = objectMapper.writeValueAsString(notification);
            // 发送到指定的队列（注意交换机和路由键）
            rabbitTemplate.convertAndSend("notificationExchange", "notification.message", message);
        } catch (Exception e) {
            // 处理发送异常
            e.printStackTrace();
        }
    }


}
